<?php
class Users_model extends CI_Model {

	function validate(){
		$return = false;
		$pass = $this->input->post('password', true);
		$this->db->where('email', $this->input->post('email', true));
		$this->db->where('password', sha1(md5($pass)));
		$query = $this->db->get('mlme_users');
		if( $query->num_rows() == 1 ){
			$return = true;
		}
		return $return;
	}
	function create_users(){
		$params = array();
		$new_user = array(
			'firstname' => $this->input->post('reg-full-name'),
			'lastname' => $this->input->post('reg-last-name'),
			'middle' => $this->input->post('reg-middle-name'),
			'email' => $this->input->post('reg-email'),
			'password' => sha1(md5($this->input->post('reg-password'))),
			'type' => $this->input->post('type'),
			'status' => 0		//by default
		);
		$new_user['date_added'] = $new_user['date_updated'] = date('Y-m-d h:i:s');
		$insert = $this->db->insert('mlme_users', $new_user);
		$id = $this->db->insert_id();
		$query = $this->db->query("select sha1(md5('$id')) as userid");
        $query_data= $query->row();
		$userid = $query_data->userid;
		$params['userid'] = $userid;
		$params['real_userid'] = $id;
		$params['insert'] = $insert;
		return $params;
	}

	function check_email($email=null){
		if(empty($email)) return FALSE;
		$query = $this->db->query("SELECT `email` FROM `mlme_users` WHERE `email` = '".$email."'");
		if( $query->num_rows() > 0 ){
			 return TRUE;
		}else {
		 	return FALSE;
		 }
	}
	function get_user(){
		$email = isset($_SESSION['email'])? $_SESSION['email']:'';
		$query = $this->db->query("SELECT * FROM `mlme_users` WHERE `email` = '".$email."'");
		return $query->row();
	}
	function getFullname( $id=null ){
		if(empty($id)) return false;
		$query = 'SELECT `firstname`,`lastname`, `middle`,`eulogy_video` FROM `mlme_users` WHERE `userid`='.$id.' LIMIT 1';
		return $this->db->query($query);
	}
	function verify_account($encrypted_id){
		$data = array("status"=>1,"date_updated"=>date('Y-m-d H:i:s'));
		$this->db->where("sha1(md5(userid))", $encrypted_id);
		$this->db->where("status", 0);
		$update = $this->db->update("mlme_users", $data);
		if($update){
			$query = $this->db->query("select email from mlme_users where sha1(md5(userid))='".$encrypted_id."' limit 1 ");
			if($query->num_rows() == 1){
				  $query_data = $query->row();
				  $_SESSION['email'] = $query_data->email;
				  $_SESSION['sess_con_msg'] = 1;//0 - do not display confirmation message 1 - display confirmation message
			}//end if
		}
		return ($update)? true:false;
	}
	function check_verified_account($email,$password){
		$return = array();
		$query = $this->db->query("SELECT type,status FROM mlme_users WHERE email='".$email."' AND password='".sha1(md5($password))."'");
		$result = $query->row_array();
		if($query->num_rows() > 0 && $result['status'] == 1){
			$return['is_verified'] = true;
		}else{
			$return['is_verified'] = false;
		}//end if
		$return['type'] = isset($result['type'])?$result['type']:'';
		return $return;
	}//end if
	function check_exist_email($email){
		$query = $this->db->query("SELECT email FROM mlme_users WHERE email = '".$email."'");
		return ($query->num_rows() > 0)? true:false;
	}//end fct
	function update_user_password($email,$new_pass){
		$qry = $this->db->query("select sha1(md5('$new_pass')) as new_pass");
        $qry_data = $qry->row();
        $new_pass = $qry_data->new_pass;
		$data = array('password' => $new_pass,'date_updated' => date('Y-m-d H:i:s'));
		$this->db->where('email',$email);
		$update = $this->db->update('mlme_users',$data);
		return ($update)? true:false;
	}//end if
	function edit_profile($post){
		$userid =Myuser_helper::get_userid($_SESSION['email']);
		$this->db->where('userid', $userid);
		$update = $this->db->update('mlme_users', $post);
		return $update;
	}
	function get_user_eulogy_vid($userid){
		$query = $this->db->query('SELECT eulogy_video FROM mlme_users WHERE userid = '.$userid);
		$result = $query->row_array();
		return $result['eulogy_video'];
	}
	function get_user_type($email){
		$userid =Myuser_helper::get_userid($email);
		$query = $this->db->query('SELECT type FROM mlme_users WHERE userid = '.$userid);
		$result = $query->row_array();
		return $result['type'];
	}//end fct
	function get_freeuser_messages($infos,$specific,$userid){
		$this->load->library('encryption_lib');
		$where = '';
		$x = 0;
		if($specific){
			if(empty($userid)){
				foreach($infos as $info){
					$x++;
					$where .= 'a.userid='.$info['userid'].' AND (a.messageid = b.messageid AND b.recipientid ='.$info['recipientid'].')';
					$where .= (count($infos) > $x)? ' OR ':'';
				}//end foreach
				$query = $this->db->query("SELECT a.* FROM mlme_messages a,mlme_messages_recipients_x b WHERE a.totype='specific' AND ".$where);
			}else{
				$query = $this->db->query("SELECT a.* FROM mlme_messages a,mlme_messages_recipients_x b WHERE a.totype='specific' AND a.messageid = b.messageid AND a.userid=".$this->encryption_lib->decode($userid));
			}//end if
		}else{
			if(empty($userid)){
				foreach($infos as $info){
					$x++;
					$where .= 'userid='.$info['userid'];
					$where .= (count($infos) > $x)? ' OR ':'';
				}//end foreach
				$query = $this->db->query("SELECT * FROM mlme_messages WHERE (totype='all' OR totype='public') AND ".$where);
			}else{
				$query = $this->db->query("SELECT * FROM mlme_messages WHERE (totype='all' OR totype='public') AND userid=".$this->encryption_lib->decode($userid));
			}//end if
		}
		return $query->result_array();
	}//end fct
	function get_recipient_user_id($email){
		$query = $this->db->query("SELECT a.recipientid,a.userid,b.firstname,b.lastname,b.picture FROM mlme_recipients a LEFT JOIN mlme_users b ON a.userid = b.userid WHERE a.email='".$email."' AND is_executor <> 1");
		return $query->result_array();
	}
	function check_recipient_password($post){
		$this->load->library('encryption_lib');
		$query = $this->db->query("SELECT email FROM mlme_users WHERE userid=".$this->encryption_lib->decode($post['userid'])." AND password_recipient='".$this->encryption_lib->encode($post['password'])."'");
		return ($query->num_rows() > 0)? true:false;
	}//end fct
	function get_user_info($userid){
		$query = $this->db->query("SELECT * FROM mlme_users WHERE userid=".$this->encryption_lib->decode($userid));
		return $query->row();
	}//end fct
	function check_executor_password($post){
		$password = $post['password'];
		$query = $this->db->query("SELECT email FROM mlme_users WHERE userid='".$this->encryption_lib->decode($post['userid'])."' AND password_executor='".$this->encryption_lib->encode($password)."'");
		return ($query->num_rows() > 0)? true:false;
	}//end fct

}// end of Model